<template>
  <div class="post-detail-container">
    <!-- 加载状态 -->
    <div v-if="isLoading" class="loading">
      <van-loading type="spinner" color="#1989fa" />
    </div>
    <!-- 错误状态 -->
    <div v-else-if="error" class="error">
      <p>加载帖子详情时出错，请稍后重试。</p>
      <van-button type="primary" @click="fetchPost">重试</van-button>
    </div>
    <!-- 正常显示帖子详情 -->
    <div v-else-if="post" class="post-content-wrapper">
      <div class="post-header">
        <div class="avatar">
          <img :src="post.t_user.avatar" alt="头像" />
        </div>
        <div class="user-info">
          <p class="user-name">{{ post.t_user.u_name }}</p>
          <p class="post-time">{{ formatTime(post.t_time) }}</p>
        </div>
      </div>

      <div class="post-content">
        <h2 class="post-title">{{ post.t_title }}</h2>
        <p class="post-text">{{ post.t_content }}</p>
      </div>

      <div class="post-footer">
        <div class="likes">
          <van-icon name="like-o" />
          <span>{{ post.likes_count }}</span>
        </div>

        <div v-if="post.lianjie" class="product-link">
          <a :href="post.lianjie" target="_blank">查看商品 ></a>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import http from '../../http/index';
import { Icon as VanIcon, Loading, Button } from 'vant';
import dayjs from 'dayjs';

const route = useRoute();
const post = ref(null);
const isLoading = ref(true);
const error = ref(null);

const formatTime = (time) => {
  return dayjs(time).format('YYYY-MM-DD HH:mm');
};

const fetchPost = async () => {
  isLoading.value = true;
  error.value = null;
  try {
    const res = await http.get(`/home/tiezi/${route.query.id}/`);
    post.value = res.data;
  } catch (err) {
    error.value = err;
  } finally {
    isLoading.value = false;
  }
};

onMounted(() => {
  fetchPost();
});
</script>

<style scoped>
.post-detail-container {
  padding: 20px;
  background: white;
  min-height: 100vh;
}

.loading {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
}

.post-content-wrapper {
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.post-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  overflow: hidden;
  margin-right: 15px;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-info {
  display: flex;
  flex-direction: column;
}

.user-name {
  margin: 0;
  font-size: 16px;
  font-weight: bold;
  color: #333;
}

.post-time {
  margin: 0;
  font-size: 14px;
  color: #666;
}

.post-title {
  margin: 0;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 10px;
  color: #333;
}

.post-text {
  margin: 0;
  font-size: 16px;
  color: #666;
  line-height: 1.6;
}

.post-footer {
  margin-top: 30px;
  border-top: 1px solid #f5f5f5;
  padding-top: 15px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.likes {
  display: flex;
  align-items: center;
}

.likes van-icon {
  margin-right: 5px;
  color: #1989fa;
}

.product-link {
  color: #4a90e2;
  text-decoration: none;
}

.product-link:hover {
  text-decoration: underline;
}
</style>    